//
//  CD.swift
//  PDF
//
//  Created by Kellv on 2024/4/20.
//

import UIKit

class CDPreViewEditView: UIView {
    
    var controlBar: CDEditControlBar!
    var optionsview: CDEditBarToolsView!
    var didSelectHanlder: ((CDTextEditOptions, _ isApplyAll: Bool)->Void)?

    private var pageSize: CDTextEditOptions = .a4
    private var footer: CDTextEditOptions = .noFooter
    private var resultoption: CDTextEditOptions!
    private var optionType:CDTextEditOptions!
    private var isApplyAllPage = false
    lazy var applyView: CDApplyView = {
        let tmp = CDApplyView(frame: CGRect(x: 0, y: controlBar.maxY, width: self.width, height: 40))
        self.addSubview(tmp)
        tmp.isHidden = true
        tmp.applyHandler = { [weak self] isOn in
            guard let self = self else {
                return
            }
            self.isApplyAllPage = isOn
        }
        return tmp
    }()
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        self.backgroundColor = .white
        controlBar = CDEditControlBar(frame: CGRect(x: 0, y: 0, width: frame.width, height: 64))
        controlBar.updateUI(leftBtnTitle: pageSize.rawValue, rightBtnTitle: footer.rawValue.localize())
        controlBar.backgroundColor = .white
        self.addSubview(controlBar)
        
        controlBar.leftActionHandler = {[weak self] isCancel in
            guard let self = self else {
                return
            }
            if isCancel {
                //cancel
                self.optionsview.loadData(options: [.addpage,.crop,.rotate,.effect,.ocr,.waterMark,.addText,.signature,.encription,.delete])
                self.controlBar.updateUI(leftBtnTitle: pageSize.rawValue, rightBtnTitle: footer.rawValue.localize())
                if self.optionType == .effect {
                    self.applyViewDimiss()
                } else if self.optionType == .crop {
                    self.cropUp()
                }

            } else {
                //page
                optionType = .pageSize
                self.optionsview.loadData(options: [.a3,.a4,.a5,.b4,.b5])
                self.controlBar.updateUI(leftBtnTitle: "Cancel".localize(), rightBtnTitle: "Done".localize())
            }

        }
        
        
        controlBar.rightActionHandler = {[weak self] isDone in
            guard let self = self else {
                return
            }
            
            if isDone {
                guard let didSelectHanlder = self.didSelectHanlder else {
                    return
                }
                didSelectHanlder(self.resultoption, self.isApplyAllPage)
            } else {
                optionType = .footer
                self.optionsview.loadData(options: [.noFooter,.leftSide,.center,.rightSide])
                self.controlBar.updateUI(leftBtnTitle: "Cancel".localize(), rightBtnTitle: "Done".localize())
            }
        }
     
        optionsview = CDEditBarToolsView(frame: CGRect(x: 0, y: controlBar.maxY, width: frame.width, height: 80), types: [.addpage,.crop,.rotate,.effect,.ocr,.waterMark,.addText,.signature,.encription,.delete])
        self.addSubview(optionsview)
        optionsview.didSelectHanlder = {[weak self] option in
            guard let self = self else {
                return
            }
            self.optionType = option

            switch option {
            case .effect:
                self.applyViewShow()
            case .crop,.rotate,.ocr,.waterMark,.addText,.addpage,.signature,.encription,.delete:
                if option == .crop {
                    self.cropDown()
                }
                guard let didSelectHanlder = self.didSelectHanlder else {
                    return
                }
                didSelectHanlder(option, self.isApplyAllPage)
            default:
                self.resultoption = option

            }
        }
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    func applyViewShow() {
        let height: CGFloat = UIDevice.safeAreaBottom() > 0 ? 192 : 160
        self.controlBar.updateUI(leftBtnTitle: "Cancel".localize(), rightBtnTitle: "Done".localize())
        self.optionsview.loadData(options: [.effect1,.effect2,.effect3,.effect4,.effect5])

        self.height = height + 40
        self.minY = CDViewHeight - self.height
        self.applyView.isHidden = false
        self.optionsview.minY = self.applyView.maxY
    }
    
    func applyViewDimiss() {
        let height: CGFloat = UIDevice.safeAreaBottom() > 0 ? 192 : 160

        if self.minY == CDViewHeight - height {
            return
        }
        self.applyView.isHidden = true
        self.height = height
        self.minY = CDViewHeight - self.height
        self.optionsview.minY = self.controlBar.maxY
    }
    
    func cropDown() {
        let height: CGFloat = UIDevice.safeAreaBottom() > 0 ? 192 : 160
        self.controlBar.updateUI(leftBtnTitle: "Cancel".localize(), rightBtnTitle: "Done".localize())

        UIView.animate(withDuration: 0.25) {
            self.minY = CDViewHeight - (height - 80)
            self.optionsview.isHidden = true
        }
    }
    
    func cropUp() {
        let height: CGFloat = UIDevice.safeAreaBottom() > 0 ? 192 : 160
        if self.minY == CDViewHeight - height {
            return
        }
        self.optionsview.isHidden = false
        UIView.animate(withDuration: 0.25) {
            self.minY = CDViewHeight - height
        }
    }
}
